

/**
 * 详情对话框
 */
var InfoDlg = {
    data: {
        isSort: null,
        nodes: []
    }
};


layui.use(['table', 'admin', 'tree', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var tree = layui.tree;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--部门管理
     */
    var Dept = {
        tableId: "deptTable",
        condition: {
            id: null,
            searchWords: null
        }
    };

    /**
     * 初始化表格的列
     */
    Dept.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'simpleName', sort: true, title: '部门简称', width: '20%'},
            {field: 'fullName', sort: true, title: '部门全称', width: '20%'},
            {field: 'description', title: '描述', width: '40%'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 120}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Dept.search = function () {
        var queryData = {};
        queryData['id'] = Dept.condition.id;
        queryData['searchWords'] = $("#searchWords").val();
        table.reload(Dept.tableId, {where: queryData});
    };

    /**
     * 弹出添加
     */
    Dept.openAddDept = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加部门',
            area: ['40%', '60%'],
            content: Lu.ctxPath + '/system/dept/toAdd',
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
    };

    /**
     * 点击编辑部门
     *
     * @param data 点击按钮时候的行数据
     */
    Dept.onUpdateDept = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改部门',
            area: ['40%', '60%'],
            content: Lu.ctxPath + '/system/dept/toUpdate?id=' + data.id,
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
    };

    /**
     * 点击删除部门
     *
     * @param data 点击按钮时候的行数据
     */
    Dept.onDeleteDept = function (data) {
        var operation = function () {
            Dept.deleteAjax(data.id);
        };
        Lu.confirm("是否删除部门 " + data.simpleName + "?", operation);
    };

    /**
     * 执行删除
     * @param id
     */
    Dept.deleteAjax = function(id){
        var ajax = new $ax(Lu.ctxPath + "/system/dept/delete", function () {
            Lu.success("删除成功!");
            tableReload();
        }, function (data) {
            Lu.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("deptId", id);
        ajax.start();
    }

    /**
     * 点击排序按钮
     *
     * @param data 点击按钮时候的行数据
     */
    Dept.onSortDept = function (data) {
        var isSort = encodeURIComponent('parent.InfoDlg.data.isSort');
        var nodes = encodeURIComponent('parent.InfoDlg.data.nodes');
        var treeUrl = encodeURIComponent('/system/dept/deptTreeList');
        layer.open({
            type: 2,
            title: '父级菜单',
            area: ['40%', '90%'],
            content: '/system/treeSort?isSort='+isSort+'&nodes='+nodes+'&treeUrl='+treeUrl+'',
            end: function () {
                var isSort = InfoDlg.data.isSort;
                var nodes = InfoDlg.data.nodes;
                if(isSort && nodes && isSort == 1){
                    //排序
                    var ajax = new $ax(Lu.ctxPath + "/system/dept/sort", function () {
                        Lu.success("排序成功!");
                        InfoDlg.data.isSort = null;
                        InfoDlg.data.nodes = null;
                        treeAjax.start();
                    }, function (data) {
                        Lu.error("排序失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("nodes", JSON.stringify(nodes));
                    ajax.start();
                }
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Dept.tableId,
        url: Lu.ctxPath + '/system/dept/listPage',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Dept.initColumn()
    });

    var tableReload = () => {
        table.reload(Dept.tableId);
        treeAjax.start();
    };

    //layuiTree
    var treeAjax = new $ax(Lu.ctxPath + "/system/dept/deptLayuiTree", function (data) {
        tree.render({
            elem: '#layuiTree'
            ,data: data
            ,onlyIconControl: true
            ,edit: ['del'] //操作节点的图标 'add', 'update',
            ,text: {
                defaultNodeName: '未命名'  //节点默认名称
                ,none: '无数据'            //数据为空时的提示文本
            }
            ,click: function(obj){
                Dept.condition.id = obj.data.id;
                Dept.search();
            }
            ,operate: function(obj){
                var type = obj.type; //得到操作类型：add、edit、del
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素
                var id = data.id;    //得到节点索引
                if(type === 'add'){  //增加节点
                    return id;
                } else if(type === 'update'){ //修改节点
                    console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容
                    data.simpleName = d.title;

                } else if(type === 'del') { //删除节点
                    Dept.deleteAjax(data.id);
                }
             }
        });
    }, function (data) {
        Lu.error("机构树获取失败!" + data.responseJSON.message + "!");
    });
    treeAjax.type = "GET";
    treeAjax.start();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Dept.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Dept.openAddDept();
    });

    //菜单排序
    $('#sort').click(function () {
        Dept.onSortDept();
    });

    // 工具条点击事件
    table.on('tool(' + Dept.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'update') {
            Dept.onUpdateDept(data);
        } else if (layEvent === 'delete') {
            Dept.onDeleteDept(data);
        }
    });
});
